var express = require('express');
var router = express.Router();
let Docs = require('../models/Docs');
let DocClassifies = require('../models/DocClassifies');

/* GET home page. */
router.get('/', function(req, res, next) {
  var counter = 0;
  var total = 0;
  function render(docs) {
      if (counter === total) {
          res.render('index/index', { title: '首页' ,docs:docs});
      }
  }

  DocClassifies.getRecommendList(function (e,classifyList) {
      if (e) {
        return render([]);
      } else {
        total = classifyList.length;
        var list = [];
        classifyList.forEach(function (item) {
            Docs.getByClassifyAndLimit(item._id,10,function (e,ds) {
                if (e) {} else {
                    list.push({classify:item,docs:ds});
                }
                counter++;
                render(list);
            });

        });
      }
  });
});

module.exports = router;
